/*
 * Copyright 2017 Matthew Tamlin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.matthewtamlin.fortytwo.library_tests;

import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ComponentState;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;

import com.matthewtamlin.fortytwo.library.answer_view.AlphaDecorator;
import com.matthewtamlin.fortytwo.library.answer_view.AlphaDecorator.AlphaSupplier;
import com.matthewtamlin.fortytwo.library.answer_view.ColorFadeDecorator;
import com.matthewtamlin.fortytwo.library.answer_view.ColorFadeDecorator.ColorSupplier;
import com.matthewtamlin.fortytwo.library.answer_view.DecoratedAnswerCard;

/**
 * A test harness for displaying and interacting with a {@link DecoratedAnswerCard}.
 */
public class DecoratedAnswerCardTestHarness extends SimpleAnswerCardTestHarness {
    /**
     * Decorates the test view by changing the background and text colors.
     */
    private final ColorFadeDecorator colorFadeDecorator = new ColorFadeDecorator(new ColorSupplier() {
        @Override
        public int getColor(final boolean marked, final boolean selected,
                            final boolean answerIsCorrect) {
            if (marked) {
                if (selected) {
                    return answerIsCorrect ? Color.GREEN.getValue() : Color.RED.getValue();
                } else {
                    return answerIsCorrect ? Color.RED.getValue() : Color.GREEN.getValue();
                }
            } else {
                return selected ? Color.BLUE.getValue() : Color.WHITE.getValue();
            }
        }
    });

    /**
     * Decorates the test view by changing the transparency.
     */
    private final AlphaDecorator alphaDecorator = new AlphaDecorator(new AlphaSupplier() {
        @Override
        public float getAlpha(final boolean marked, final boolean selected,
                              final boolean answerIsCorrect) {
            if (marked && !selected && !answerIsCorrect) {
                return 0.5f;
            } else {
                return 1f;
            }
        }
    });

    /**
     * The view under test.
     */
    private DecoratedAnswerCard testView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().setStatusBarVisibility(Component.HIDE);

        getControlsContainer().addComponent(createUseColorDecoratorButton());
        getControlsContainer().addComponent(createRemoveColorDecoratorButton());
        getControlsContainer().addComponent(createUseAlphaDecoratorButton());
        getControlsContainer().addComponent(createRemoveAlphaDecoratorButton());
        getControlsContainer().addComponent(createClearDecoratorsButton());
    }

    @Override
    public DecoratedAnswerCard getTestView() {
        if (testView == null) {
            testView = new DecoratedAnswerCard(this);
            testView.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT));
            testView.setMinHeight(AttrHelper.vp2px(50, this));
        }

        return testView;
    }

    /**
     * Creates a button which adds the color fade decorator to the test view when clicked.
     *
     * @return the button
     */
    private Button createUseColorDecoratorButton() {
        final Button b = new Button(this);
        configBtn(b);
        b.setText("Use color fade decorator");

        b.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getTestView().addDecorator(colorFadeDecorator, true);
            }
        });

        return b;
    }

    /**
     * Creates a button which removes the color fade decorator from the test view when clicked.
     *
     * @return the button
     */
    private Button createRemoveColorDecoratorButton() {
        final Button b = new Button(this);
        configBtn(b);
        b.setText("Remove color fade decorator");

        b.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getTestView().removeDecorator(colorFadeDecorator);
            }
        });

        return b;
    }

    /**
     * Creates a button which adds the alpha decorator to the test view when clicked.
     *
     * @return the button
     */
    private Button createUseAlphaDecoratorButton() {
        final Button b = new Button(this);
        configBtn(b);
        b.setText("Use alpha decorator");

        b.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getTestView().addDecorator(alphaDecorator, true);
            }
        });

        return b;
    }

    /**
     * Creates a button which removes the alpha decorator from the test view when clicked.
     *
     * @return the button
     */
    private Button createRemoveAlphaDecoratorButton() {
        final Button b = new Button(this);
        configBtn(b);
        b.setText("Remove alpha decorator");

        b.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getTestView().removeDecorator(alphaDecorator);
            }
        });

        return b;
    }

    /**
     * Creates a button which removes all decorators from the test view when clicked.
     *
     * @return the button
     */
    private Button createClearDecoratorsButton() {
        final Button b = new Button(this);
        configBtn(b);
        b.setText("Clear decorators");
        b.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getTestView().clearDecorators();
            }
        });

        return b;
    }

    private void configBtn(Button b) {
        int padding = 20;
        int paddingTop = 25;
        final int textSize = 40;
        b.setPadding(padding, paddingTop, padding, paddingTop);
        b.setTextSize(textSize);
        b.setTextColor(new Color(Color.getIntColor("#333333")));

        StateElement stateElement = new StateElement();
        ShapeElement selectedElement = new ShapeElement(getContext(), ResourceTable.Graphic_btn_bg_press);
        ShapeElement emptyElement = new ShapeElement(getContext(), ResourceTable.Graphic_btn_bg_normal);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, selectedElement);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, emptyElement);
        b.setBackground(stateElement);

        b.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT));

        int margin = 10;
        b.setMarginRight(margin);
        b.setMarginBottom(margin);
        b.setMarginLeft(margin);
        b.setMarginTop(margin);

        b.setFont(Font.DEFAULT_BOLD);
    }
}